﻿/************************************************
* Copyright (C) 2011 东北农业大学(NEAU) 版权所有。
* 文件名：
* 版本号：
* 文件功能描述：
* 作者：
* 创建日期:
* 版本号：
* 修改人：
* 修改日期：
* 修改描述：
************************************************/
Ext.BLANK_IMAGE_URL = '/script/ext/resources/images/s.gif';
Ext.onReady(function () {
    Ext.QuickTips.init();
    /*proxy*/
    var proxy = new Ext.data.HttpProxy({ url: "SERVER/module/MenuList.aspx" });
    /*定义数据模型*/
    var UserTreeMenu = new Ext.data.Record.create([{ name: 'ID', type: 'string' }, { name: 'Text', type: 'string' }, { name: 'Href', type: 'string' }, { name: 'IconCls', type: 'string' }, { name: 'Cls', type: 'string' }, { name: 'IsClass', type: 'bool' }, { name: 'ParentID', type: 'int' }, { name: 'Sort', type: 'int'}]);
    /*reader*/
    var reader = new Ext.data.JsonReader({ totalProperty: "Count", root: "List" }, UserTreeMenu);
    /*groupStore*/
    var groupStore = new Ext.data.GroupingStore({ proxy: proxy, reader: reader, sortInfo: { field: 'Sort', direction: 'DESC'} });
    /*设置每页条数*/
    var pageSize = 30; var cookiePageSize = parseInt(Ext.util.Cookies.get("pageSize")); if (cookiePageSize) { pageSize = cookiePageSize; }
    /*加载数据源*/
    groupStore.load({ params: { start: 0, limit: pageSize} });
    /*定义列模型*/
    var cm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), {
        id: 'ID',
        header: 'ID',
        dataIndex: 'ID',
        width: 50,
        sortable: true,
        editor: { disabled: true }
    }, {
        id: 'Text',
        header: '显示名称',
        dataIndex: 'Text',
        width: 100,
        sortable: true,
        editor: { allowBlank: false }
    }, {
        xtype: 'booleancolumn',
        header: '启用样式',
        dataIndex: 'IsClass',
        width: 72,
        editor: { xtype: 'checkbox' }
    }, {
        id: 'Cls',
        header: '样式',
        dataIndex: 'Cls',
        width: 70,
        sortable: true,
        editor: { xtype: 'textfield' }
    }, {
        id: 'IconCls',
        header: '图标样式',
        dataIndex: 'IconCls',
        width: 100,
        sortable: true,
        editor: { xtype: 'textfield' }
    }, {
        id: 'ParentID',
        header: '父编号',
        dataIndex: 'ParentID',
        width: 70,
        sortable: true,
        editor: { allowBlank: false }
    }, {
        id: 'Sort',
        header: '排序权重',
        dataIndex: 'Sort',
        width: 85,
        sortable: true,
        editor: { allowBlank: false }
    }, {
        id: 'Href',
        header: '接口',
        dataIndex: 'Href',
        width: 300,
        sortable: true,
        editor: { allowBlank: false }
    }]);

    //创建行编辑模式
    var editor = new Ext.ux.grid.RowEditor({});
    //行编辑模式更新事件
    editor.on({
        scope: this,
        afteredit: function (roweditor, changes, record, rowIndex) {
            //自定义参数
            var addparams = {
                Text: record.get("Text"),
                Href: record.get("Href"),
                IconCls: record.get("IconCls"),
                Cls: record.get("Cls"),
                IsClass: record.get("IsClass"),
                ParentID: record.get("ParentID"),
                Sort: record.get("Sort")
            };
            Ext.Ajax.request({
                url: record.get('ID') == "" ? "SERVER/module/MenuAdd.aspx" : "SERVER/module/MenuAdd.aspx?id=" + record.get('ID'),
                method: 'POST',
                params: record.get('ID') == "" ? addparams : changes,
                success: function (response) {
                    if (record.get('ID') == "")
                        record.set("ID", Ext.util.JSON.decode(response.responseText).id);
                }
            });
        }
    });
    //创建下拉框
    var comboBox = new Ext.form.ComboBox({ width: 60, typeAhead: true, triggerAction: 'all', lazyRender: true, mode: 'local', title: "每页行数", emptyText: pageSize, store: new Ext.data.ArrayStore({ id: 0, fields: ['myId', 'displayText'], data: [[10, 10], [20, 20], [30, 30], [50, 50], [100, 100]] }), valueField: 'myId', displayField: 'displayText' }); comboBox.on("select", function (combo, record, index) { Ext.util.Cookies.set("pageSize", record.id); window.location.reload(); });
    //创建表格
    var grid = new Ext.grid.GridPanel({
        store: groupStore,
        border: false,
        region: 'center',
        autoExpandColumn: "Href",
        loadMask: { msg: '正在努力的加载，请稍等片刻..' },
        plugins: [editor],
        view: new Ext.grid.GroupingView(),
        cm: cm,
        tbar: [{
            text: "信息检索",
            iconCls: "icon-search-16",
            handler: function () {
                alert("信息检索");
            }
        }, {
            iconCls: 'icon-add',
            text: '添加',
            handler: function () {
                var e = new UserTreeMenu({
                    Text: "新模块名称1",
                    Href: "#",
                    IconCls: "icon-plugin-16",
                    Cls: "package",
                    IsClass: true,
                    ParentID: 0,
                    Sort: 0
                });
                editor.stopEditing();
                groupStore.insert(0, e);
                grid.getView().refresh();
                grid.getSelectionModel().selectRow(0);
                editor.startEditing(0);
            }
        }, {
            ref: '../removeBtn',
            iconCls: 'icon-del',
            text: '删除',
            disabled: true,
            handler: function () {
                Ext.MessageBox.confirm("确认提示", "确认要删除所选行数据？", function (btn) {
                    if (btn == "yes") {
                        editor.stopEditing();
                        var s = grid.getSelectionModel().getSelections();
                        var delR = new Array();
                        var delRid = new Array();
                        for (var i = s.length - 1; i >= 0; i--) {
                            var r = s[i];
                            if (r.get('ID') != undefined) {
                                delRid.push(r.get('ID'));
                                delR.push(r);
                            }
                        }
                        Ext.Ajax.request({
                            url: "SERVER/module/MenuDel.aspx",
                            method: 'POST',
                            params: { id: delRid },
                            success: function (response) {
                                var json = Ext.util.JSON.decode(response.responseText);
                                for (var i = 0; i < delR.length; i++) {
                                    if (json.ids.valueOf(delR[i].get('ID'))) {
                                        groupStore.remove(delR[i])
                                    }
                                }
                                grid.getView().refresh();
                                grid.getSelectionModel().selectRow(0);
                                Ext.Msg.alert("提示", "已删除 <b>" + json.count + "</b> 行<br/>" + json.msg, function () {
                                    window.location.reload();
                                });
                            }
                        });
                    }
                });
            }
        }],
        bbar: new Ext.PagingToolbar({
            store: groupStore,
            pageSize: pageSize,
            displayInfo: true,
            prependButtons: true,
            items: ['每页行数', comboBox, '-'],
            displayMsg: "本页显示第{0}条到第{1}条数据，共{2}条",
            emptyMsg: "暂无记录"
        })
    });
    /*创建页面布局*/
    var viewport = new Ext.Viewport({ layout: 'border', items: [grid] }); viewport.doLayout();
    setTimeout(function () { Ext.get('loading').remove(); Ext.get('loading-mask').fadeOut({ remove: true }); }, 400);
    //有选择行启用删除按钮
    grid.getSelectionModel().on('selectionchange', function (sm) { grid.removeBtn.setDisabled(sm.getCount() < 1); });
});